using UnityEngine;

// 给物体加上刚体组件
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 12.0f;

    private Vector2 moveDirection;
    private Rigidbody2D rigidbody2D;

    // 给人物加上动画控制
    private Animator animator;

    private void Awake() {
        rigidbody2D = GetComponent<Rigidbody2D>();

        animator = GetComponent<Animator>();
    }
    private void Update() {
        moveDirection = new Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));
        moveDirection *= moveSpeed;

        // TODO: 动画
        // 人物的速度
        float playerMoveSpeed = Mathf.Abs(moveDirection.x)+Mathf.Abs(moveDirection.y);
        animator.SetFloat(StringHash.MoveSpeed,playerMoveSpeed);
        // Debug.Log("速度"+playerMoveSpeed);

    }
    private void FixedUpdate() {
        rigidbody2D.AddForce(moveDirection,ForceMode2D.Impulse); // 顺时力，考虑质量
    }

}
